import {routerRedux} from 'dva/router';
import api from '../api';
import {time} from '../config'
import moment from 'moment'

let timer

export default {
  namespace : 'realtimeStrategy',
  state : {
  },
  subscriptions : {
    setup({dispatch, history}) {
      history.listen(({pathname,query}) => {
        clearInterval(timer)
        if( pathname === '/tradeAnalysis/tca/realtimeStrategy' ){
          dispatch({type: 'requestResult',payload:query})
          timer=setInterval(() => {
            const nowTime=moment().format('H.m')
            if(nowTime>9.30&&nowTime<15.30){
              dispatch({type: 'requestResult',payload:query})
            }
      		}, time)
        }
      })
    }
  },
  effects : {
    * requestResult({payload}, {call, put,select}){
      const response = yield call(api.tca.getRealtimeStrategy, payload);
      yield put({type:'clearState'})
      yield put({type: 'querySuccess', payload: response});
    },
    * changeRouter({payload}, {put}){
      clearInterval(timer)
      yield put(routerRedux.push(payload))
    },
  },
  reducers : {
    clearState(state,{payload}) {
      return{
        ...state,
        orderNo:null,
        pie:null,
        trendChart:null
      }

    },
    querySuccess(state, {payload}) {

      return {
        ...state,
        ...payload.data
      }
    }
  }
};
